/*
 * Copyright 2006-2007 Queplix Corp.
 *
 * Licensed under the Modified Queplix BSD License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.queplix.com/BSDLICENSE-1.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.jlect.swebing.ui.client.menu;

import com.jlect.swebing.renderers.client.*;
import com.jlect.swebing.ui.client.Dimension;
import com.jlect.swebing.ui.client.GContainer;
import com.jlect.swebing.ui.client.LayoutManager;

/**
 * Small lightweight component that could be shown on the screen. It doesn't
 * have any controls to hide it, instead of it this component is being closed
 * by clicking outside of it, or typing some close events, or focusing out of it.
 *
 * @author Sergey Kozmin
 * @since Nov 26, 2007
 */
public class GPopupMenu extends GContainer implements PopupListener {
    private PopupListenerCollection listeners;

    public GPopupMenu(LayoutManager layoutManager) {
        super(layoutManager);
        setPreferredSize(new Dimension(200, 200));//todo change
    }

    public GPopupMenu() {
        this(null);
    }

    public void setVisible(boolean visible) {
        getPopupRenderer().setVisible(visible);
    }

    public boolean isVisible() {
        return getPopupRenderer().isVisible();
    }

    public void addPopupListener(PopupListener listener) {
        if(listeners == null) {
            listeners = new PopupListenerCollection();
        }
        listeners.add(listener);
    }

    public void removePopupListener(PopupListener listener) {
        if(listeners != null) {
            listeners.remove(listener);
        }
    }

    public PopupPanelRenderer getPopupRenderer() {
        return (PopupPanelRenderer) getRenderer();
    }

    protected Renderer createRenderer() {
        PopupPanelRenderer renderer = (PopupPanelRenderer) RenderersConfig.getRenderersFactory().createComponentRenderer(DefaultRenderers.POPUP_PANEL.getName());
        renderer.setPopupListener(this);
        return renderer;
    }

    public void popupBecomeVisible(boolean isVisible) {
        if(listeners != null) {
            listeners.firePopupVisibilityChanged(isVisible);
        }
    }
}
